<template>
  <div class="app-container">
    <div class="filter-container">
      <!-- <el-input v-model.trim="query.invoiceCode" clearable placeholder="请输入发票号" @keyup.enter.native="getInfo" />
      <el-button type="primary" icon="el-icon-search" @click="getInfo">
        {{ $t('table.search') }}
      </el-button> -->
    </div>
    <el-table :data="tableData" row-key="index" highlight-current-row border
      :header-cell-style="{ 'text-align': 'center' }" :cell-style="{ 'text-align': 'center' }">
      <el-table-column prop="invoiceCode" label="发票号" />
      <el-table-column prop="invoiceDate" label="发票日期" />
      <el-table-column prop="ourHeaderId" label="公司方抬头">
        <template slot-scope="{row:{ourHeader}}">
          {{ ourHeader.name }}/{{ ourHeader.taxpayer }}/{{ ourHeader.bankDetail }}
        </template>
      </el-table-column>
      <el-table-column prop="sellHeaderId" label="销售方抬头">
        <template slot-scope="{row}">
          {{ row.beneficiaryAccountName }}/{{ row.bankAccount }}/{{ row.bankName }}
        </template>
      </el-table-column>
      <!-- <el-table-column prop="taxRate" label="税率" /> -->
      <el-table-column prop="taxAmount" label="税额" />
      <el-table-column prop="amount" label="金额（不含税）" />
      <el-table-column prop="totalAmount" label="金额（含税）" />
      <el-table-column prop="isPay" label="是否已收">
        <template slot-scope="{row:{isPay}}">
          <el-tag :type="isPay == 1 ? 'success' : 'danger'">{{ isPay == 1 ? '是' : '否' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="ys" label="未使用预收金额" />
      <el-table-column :label="$t('table.actions')" align="center">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" :disabled="row.isPay == 1" @click="handleClick(row)">核销</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog @closed="fundHandleClose" :close-on-click-modal="false" :visible.sync="fundDialogVisible" title="核销">
      <el-form ref="fundRuleForm" :model="fund" label-width="140px" :rules="fundRules">
        <el-row>
          <el-col :span="12">
            <el-form-item label="资金账号" prop="accountId">
              <el-select style="width: 100%;" v-model="fund.accountId" clearable placeholder="请选择资金账号" filterable>
                <el-option v-for="item in accountList" :key="item.id" :label="`${item.bankName}/${item.bankAccount}`" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="使用预收金额" prop="prePrice">
              <el-input v-model.trim="fund.prePrice" @input="fund.prePrice=fund.prePrice.replace(/[^\d|\.]/g, '')" placeholder="请输入核销金额" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="金额（含税）" prop="totalAmount">
              <el-input :disabled="true" v-model.trim="fund.totalAmount" placeholder="请输入金额（含税）" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="danger" @click="fundDialogVisible = false">{{ $t('permission.cancel') }}</el-button>
        <el-button type="primary" @click="fundConfirm">{{ $t('permission.save') }}</el-button>
      </div>
    </el-dialog>
    <el-pagination style="margin-top:20px;" :current-page="query.pageIndex" :page-sizes="[5, 10, 20, 50]"
      :page-size="query.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  </div>
</template>
<script>
import { accPreReceivable, accPay } from '@/api/evaluatePerformance'
import { accountGetAll } from '@/api/product'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'Customerforecast',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      dialogVisible: false,
      query: {
        pageIndex: 1,
        pageSize: 20
      },
      tableData: [],
      fundDialogVisible: false,
      fund: {},
      fundRules: {
        accountId: [
          { required: true, message: '请选择资金账户', trigger: 'change' }
        ],
        prePrice: [
          { required: true, message: '请输入核销金额', trigger: 'blur' }
        ]
      },
      accountList:[]
    }
  },
  mounted() {
    this.getInfo()
    this.getAccountGetAll()
  },
  methods: {
    fundHandleClose(){
      this.$refs['fundRuleForm'].resetFields();
    },
    async getAccountGetAll(){
      this.accountList = await accountGetAll()
    },
    fundConfirm() {
      this.$refs['fundRuleForm'].validate(async (valid) => {
        if (valid) {
          const {accountId,prePrice,id} = this.fund
          await accPay({
            accountId:accountId,
            prePrice:prePrice,
            sellInvoiceId:id
          })
          this.getInfo()
          this.$message.success('操作成功！')
          this.fundDialogVisible = false
        }
      })
    },
    async handleClick(item) {
      this.fund = item
      this.$set(this.fund,'prePrice',item.ys)
      this.fundDialogVisible = true
    },
    handleSizeChange(size) {
      this.query.pageSize = size
      this.getInfo()
    },
    handleCurrentChange(currentPage) {
      this.query.pageIndex = currentPage
      this.getInfo()
    },
    async getInfo() {
      const { list, total } = await accPreReceivable(this.query)
      this.tableData = list
      this.total = total
    }
  }
}

</script>
<style>
.item {
  padding: 10px 30px;
  border: 0.5px solid #dfe6ec;

}

.name {

  font-weight: bold;
}

.item1 {
  /*color:#999;*/
  /*font-size:14px;*/
}
</style>
